package com.streambolics.std;

import java.util.ArrayList;

/**
 * A TextBuilder that delegates to other TextBuilders.
 * <p>
 * This is useful to create multiple representations of the same text, such as a
 * multipart MIME e-mail message.
 * </p>
 * 
 * @author Sauron
 * 
 */

public class CompositeTextBuilder implements TextBuilder
{
    private final ArrayList<TextBuilder> _Builders = new ArrayList<TextBuilder> ();

    /**
     * Creates a CompositeTextBuilder with an empty set of delegates.
     */

    public CompositeTextBuilder()
    {
    }

    @Override
    public void addAnchor (String aURL, String aText)
    {
        for (TextBuilder t : _Builders)
        {
            t.addAnchor (aURL, aText);
        }
    }

    /**
     * Adds a child to the list of delegates.
     * <p>
     * Normally, all children must be added before any text is appended to the
     * builder. Children appended after text has been appended will not receive
     * the text appended before they were added.
     * </p>
     * 
     * @param aChild
     *            The child to add.
     */

    public void addChild (TextBuilder aChild)
    {
        _Builders.add (aChild);
    }

    @Override
    public void addParagraph (String... aTexts)
    {
        for (TextBuilder t : _Builders)
        {
            t.addParagraph (aTexts);
        }
    }

    @Override
    public void addText (String... aTexts)
    {
        for (TextBuilder t : _Builders)
        {
            t.addText (aTexts);
        }
    }

    @Override
    public void closeParagraph ()
    {
        for (TextBuilder t : _Builders)
        {
            t.closeParagraph ();
        }
    }

    @Override
    public void openParagraph ()
    {
        for (TextBuilder t : _Builders)
        {
            t.openParagraph ();
        }
    }
}
